package com.loongtech.bi.action.flow;

import com.loongtech.bi.action.Base2BiSearchAction;
import com.loongtech.bi.action.UIHelper;
import com.loongtech.bi.manager.flow.HadoopQueryManager;
import com.loongtech.bi.manager.system.GameParamConfigManager;
import com.loongtech.bi.manager.system.SysLogManager;
import com.loongtech.bi.support.CommonGameParam;
import com.loongtech.bi.support.Session;
import com.loongtech.core.util.Time;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

/**
 * 流水查询—充值流水Action
 * @author Marin
 * @date 2015年09月17日
 */
@Controller
@Scope("view")
public class RechargeFlowAction extends Base2BiSearchAction {
	private static final long serialVersionUID = 1L;
	protected DecimalFormat df = new DecimalFormat("######0.00");   
	protected Date dateStart = new Date(Time.getBeginningOfDay(Time.currentTime()));
	protected Date dateEnd = new Date(Time.getBeginningOfDay(Time.currentTime()));

	private RechargeFlow queryBean;  // 查询bean
	private Map<String, String> addtypeItmes = GameParamConfigManager.getItems(CommonGameParam.RECHARGE_TYPE);  // 支付方式下拉
	private Map<String, String> addtypeItmesRevise = GameParamConfigManager.getItemsRevise(CommonGameParam.RECHARGE_TYPE);  // 支付方式下拉反转

    private Map<String, String> productItmes = GameParamConfigManager.getItems(CommonGameParam.PRODUCT_NAME);  // 商品名称下拉
    private Map<String, String> productItmesRevise = GameParamConfigManager.getItemsRevise(CommonGameParam.PRODUCT_NAME);  // 商品名称下拉反转
	
	private List<Object[]> result;  // 结果List
	
	@Autowired
	HadoopQueryManager hadoopQueryManager;
	
	@Autowired
	private SysLogManager sysLogManager;
	@Autowired
	private Session session;
	
	@PostConstruct
	public void init() {
		queryBean = new RechargeFlow();
		result = new ArrayList<>();
	}
	
	public void search()  {
		sysLogManager.addLog(session.getUserName(), "流水查询—充值流水");
		
		result = new ArrayList<>();
		
		FacesContext context = FacesContext.getCurrentInstance();
		
		// 开始结束时间不能大于当前时间60天
		if (Time.getInterval(new Date(), dateStart) > 60 || Time.getInterval(new Date(), dateEnd) > 60) {
			context.addMessage(null, new FacesMessage("提示", "开始结束时间不能小于当前时间60天!"));
			return;
		}
		
		// 外部订单号，内部订单号，用户ID，角色ID必要有一个非空
		if (StringUtils.isBlank(queryBean.getOrderid()) && StringUtils.isBlank(queryBean.getOrderid2()) && StringUtils.isBlank(queryBean.getUserid()) && StringUtils.isBlank(queryBean.getRoleid())) {
			context.addMessage(null, new FacesMessage("提示", "外部订单号，内部订单号，用户ID，角色ID必须至少有一个非空!"));
			return;
		}
		
		if (HadoopQueryManager.VISIT_QUEUE.offer("RechargeFlowAction")) {
			System.out.println("VISIT_QUEUE offer RechargeFlowAction ok!");
			
			StringBuffer sql = new StringBuffer();
			sql.append("select innerorderid,outerorderid,logtime,platform,channel,serverid,mac,userid,roleid,rolelevel,viplevel,totalcash,cashadd,yuanbaoadd,flag,addtype from addcash where 1=1 ");
            if (queryBean.getUserid() != null && !"".equals(queryBean.getUserid())) {
                sql.append(" and userid = '");
                sql.append(queryBean.getUserid().trim());
                sql.append("' ");
            }
            if (!StringUtils.isBlank(queryBean.getRoleid())) {
                sql.append(" and cast(roleid as string) = '");
                sql.append(queryBean.getRoleid());
                sql.append("' ");
            }
			if (!"0".equals(queryPlatform)) {
				sql.append(" and platform = ");
				sql.append(queryPlatform);
			}
			if (!"0".equals(queryChannel)) {
				sql.append(" and channel = '");
				sql.append(queryChannel);
				sql.append("' ");
			}
			if (!"0".equals(queryServer)) {
				sql.append(" and serverid = ");
				sql.append(queryServer);
			}
			if (queryBean.getOrderid() != null && !"".equals(queryBean.getOrderid())) {
				sql.append(" and outerorderid = '");
				sql.append(queryBean.getOrderid().trim());
				sql.append("' ");
			}
			if (queryBean.getOrderid2() != null && !"".equals(queryBean.getOrderid2())) {
				sql.append(" and innerorderid = '");
				sql.append(queryBean.getOrderid2().trim());
				sql.append("' ");
			}
			if (queryBean.getFlag() != -1) {
				sql.append(" and flag = ");
				sql.append(queryBean.getFlag());
			}
			if (queryBean.getAddtype() != -99999) {
				sql.append(" and bitand(addtype,");
                sql.append(queryBean.getAddtype());
                sql.append(")=");
				sql.append(queryBean.getAddtype());
			}
            if (queryBean.getProduct() != -99999) {
                sql.append(" and productid = ");
                sql.append(queryBean.getProduct());
            }
            sql.append(" and dt >= '");
            sql.append(Time.getDateStr(dateStart));
            sql.append("' and dt <= '");
            sql.append(Time.getDateStr(dateEnd));
            sql.append("' ");
			sql.append(" order by logtime desc ");
			
			System.out.println(sql.toString());
			Entry<List<String>, List<Object[]>> ret = hadoopQueryManager.executeSQL(sql.toString());
			if (null == ret || null == ret.getKey() || ret.getKey().isEmpty() || null == ret.getValue()) {
				context.addMessage(null, new FacesMessage("提示", "查询结果为空!"));
				return;
			}
			
			result = ret.getValue();
		} else {
			System.out.println("VISIT_QUEUE offer RechargeFlowAction failed!");
			context.addMessage(null, new FacesMessage("提示", "当前系统正被访问，为保证服务器稳定，请稍后再试"));
			return;
		}
	}
	
	// 查询充值类型
	public String getAddType(Object addType) {
		String key = addType == null ? "" : Math.abs(Integer.parseInt(addType.toString())) + "";
		return (addtypeItmesRevise.get(key) == null) ? key : addtypeItmesRevise.get(key); 
	}

    public String getProductName(Object product) {
        String key = product == null ? "" : Math.abs(Integer.parseInt(product.toString())) + "";
        return (productItmesRevise.get(key) == null) ? key : productItmesRevise.get(key);
    }
	
	public String getMoeny(Object addType){
		return null != addType ? df.format(UIHelper.getYuan(Double.valueOf(addType.toString()))) : "";
	}
	
	public class RechargeFlow {
		private String orderid;  // 外部订单号
		private String orderid2;  // 内部订单号
		private String logtime; // 时间
		private String dt; // 日期
		private int gameid; // 游戏ID
		private int platform; // 平台
		private String channel; // 渠道
		private int serverid; // 服务器ID
		private String mac; // 设备mac地址
		private String userid; // 账号ID
		private String roleid; // 角色ID
		private int totalcash; // 充值总元宝
		private int cashadd; // 充值金额(分)
		private int yuanbaoadd; // 本次增加的元宝
		private int rolelevel; // 角色等级
		private int viplevel; // VIP等级
		private int flag = -1; // 是否首次付费
		private int addtype = -99999; // 充值类型
        private int product = -99999; // 商品名称
		
		public String getLogtime() {
			return logtime;
		}

		public void setLogtime(String logtime) {
			this.logtime = logtime;
		}

		public String getDt() {
			return dt;
		}

		public void setDt(String dt) {
			this.dt = dt;
		}

		public int getGameid() {
			return gameid;
		}

		public void setGameid(int gameid) {
			this.gameid = gameid;
		}

		public int getPlatform() {
			return platform;
		}

		public void setPlatform(int platform) {
			this.platform = platform;
		}

		public String getChannel() {
			return channel;
		}

		public void setChannel(String channel) {
			this.channel = channel;
		}

		public int getServerid() {
			return serverid;
		}

		public void setServerid(int serverid) {
			this.serverid = serverid;
		}

		public String getRoleid() {
			return roleid;
		}

		public void setRoleid(String roleid) {
			this.roleid = roleid;
		}

		public String getMac() {
			return mac;
		}

		public void setMac(String mac) {
			this.mac = mac;
		}

		public String getUserid() {
			return userid;
		}

		public void setUserid(String userid) {
			this.userid = userid;
		}

		public int getTotalcash() {
			return totalcash;
		}

		public void setTotalcash(int totalcash) {
			this.totalcash = totalcash;
		}

		public int getCashadd() {
			return cashadd;
		}

		public void setCashadd(int cashadd) {
			this.cashadd = cashadd;
		}

		public int getYuanbaoadd() {
			return yuanbaoadd;
		}

		public void setYuanbaoadd(int yuanbaoadd) {
			this.yuanbaoadd = yuanbaoadd;
		}

		public int getRolelevel() {
			return rolelevel;
		}

		public void setRolelevel(int rolelevel) {
			this.rolelevel = rolelevel;
		}

		public int getViplevel() {
			return viplevel;
		}

		public void setViplevel(int viplevel) {
			this.viplevel = viplevel;
		}

		public int getFlag() {
			return flag;
		}

		public void setFlag(int flag) {
			this.flag = flag;
		}

		public int getAddtype() {
			return addtype;
		}

		public void setAddtype(int addtype) {
			this.addtype = addtype;
		}

		public String getOrderid() {
			return orderid;
		}

		public void setOrderid(String orderid) {
			this.orderid = orderid;
		}

		public String getOrderid2() {
			return orderid2;
		}

		public void setOrderid2(String orderid2) {
			this.orderid2 = orderid2;
		}

        public int getProduct() {
            return product;
        }

        public void setProduct(int product) {
            this.product = product;
        }
    }

	@Override
	protected String csvContent() {
		return null;
	}

	@Override
	protected String csvFileName() {
		return null;
	}

	public RechargeFlow getQueryBean() {
		return queryBean;
	}

	public void setQueryBean(RechargeFlow queryBean) {
		this.queryBean = queryBean;
	}

	public List<Object[]> getResult() {
		return result;
	}

	public void setResult(List<Object[]> result) {
		this.result = result;
	}

	public Map<String, String> getAddtypeItmes() {
		return addtypeItmes;
	}

	public void setAddtypeItmes(Map<String, String> addtypeItmes) {
		this.addtypeItmes = addtypeItmes;
	}
	
	public Date getDateStart() {
		return dateStart;
	}

	public void setDateStart(Date dateStart) {
		this.dateStart = dateStart;
	}
	
	public Date getDateEnd() {
		return dateEnd;
	}

	public void setDateEnd(Date dateEnd) {
		this.dateEnd = dateEnd;
	}

    public Map<String, String> getProductItmes() {
        return productItmes;
    }

    public void setProductItmes(Map<String, String> productItmes) {
        this.productItmes = productItmes;
    }
}
